﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FbKPI.Model;
using FbKPI.Facade;
using System.Web.Script.Serialization;
using System.Collections.ObjectModel;
using System.Web.Script.Services;

namespace FBKPI.Serializer
{
    /// <summary>
    /// Summary description for DashboardSerializer
    /// </summary>
    public class DashboardSerializer : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";
            string result = this.GetAllData();
            context.Response.Write(result);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private string[] Columns
        {
            get
            {
                return new string[]
                {
                    "KPIAssignID2",
                   "No",
                   "Description",
                   "Weight",
                   "Format",
                   "UoMMax",
                   "UoMMin",
                   "Jan",
                   "Feb",
                   "Mar",
                   "Apr",
                   "May",
                   "Jun",
                   "Jul",
                   "Aug",
                   "Sep",
                   "Oct",
                   "Nov",
                   "Dec",
                   "Total",
                   "ScoreAchieved",
                   "KPIAssignID"
                };
            }
        }

        private string GetAllData()
        {
            int sEcho = Convert.ToInt16(HttpContext.Current.Request.Params["sEcho"]);
            int iDisplayLength = Convert.ToInt16(HttpContext.Current.Request.Params["iDisplayLength"]);
            int iDisplayStart = Convert.ToInt16(HttpContext.Current.Request.Params["iDisplayStart"]);
            int iSortCol_0 = Convert.ToInt16(HttpContext.Current.Request.Params["iSortCol_0"]);
            string sSortDir = HttpContext.Current.Request.Params["sSortDir_0"];
            string sSearch = HttpContext.Current.Request.Params["sSearch"];
            string sSortColumn = Columns[iSortCol_0];
            Employee User = new EmployeeFacade().GetEmployeeByID(Convert.ToInt16(HttpContext.Current.Request.Params["User"]));
            int year = DateTime.Now.Year;
            string EmployeeName = User.FirstName + " " + User.LastName;

            SetupYearFacade yf = new SetupYearFacade();
            SetupYear objYear = yf.GetByID(1);
            if (objYear != null)
                year = objYear.EndYear;

            


            IList<Dashboard> result = new List<Dashboard>();
            DashboardFacade pf = new DashboardFacade();
            int iTotalRecords = 0;
            int iTotalDisplayRecords = 0;
            IDictionary<int, IDictionary<string, string>> collection = new Dictionary<int, IDictionary<string, string>>();
            string TotalWeight = string.Empty;
            string TotalScore = string.Empty;
            sysLogFacade sysLogFacade = new sysLogFacade();
            sysLog objSysLog = new sysLog();

            try
            {
                result = pf.GetDashboardData(year, User, sSearch, iDisplayStart, iDisplayLength + iDisplayStart, sSortColumn, sSortDir, out iTotalRecords);
                //iTotalRecords = result.Count;
                iTotalDisplayRecords = iTotalRecords;
                TotalWeight = pf.GetTotalWeightKPI(year, User.ID);
                TotalScore = Convert.ToDouble(pf.GetTotalScoreKPI(year, User.ID)).ToString("0.00");
               

                if (result.Count > 0)
                {

                    int idx = iDisplayStart;
                    foreach (Dashboard p in result)
                    {
                        idx++;
                        IDictionary<string, string> ColumnValues = new Dictionary<string, string>();
                        ColumnValues.Add(this.Columns[0], p.KPIAssignID.ToString());
                        ColumnValues.Add(this.Columns[1], idx.ToString());
                        ColumnValues.Add(this.Columns[2], p.KPIDescription.ToString());
                        ColumnValues.Add(this.Columns[3], p.Weight.ToString());
                        ColumnValues.Add(this.Columns[4], p.Format.ToString());
                        ColumnValues.Add(this.Columns[5], p.DescriptionUoMDetailMax.ToString());
                        ColumnValues.Add(this.Columns[6], p.DescriptionUoMDetailMin.ToString());
                        ColumnValues.Add(this.Columns[7], p.Jan.ToString() == "0" ? string.Empty : p.Jan.ToString());
                        ColumnValues.Add(this.Columns[8], p.Feb.ToString() == "0" ? string.Empty : p.Feb.ToString());
                        ColumnValues.Add(this.Columns[9], p.Mar.ToString() == "0" ? string.Empty : p.Mar.ToString());
                        ColumnValues.Add(this.Columns[10], p.Apr.ToString() == "0" ? string.Empty : p.Apr.ToString());
                        ColumnValues.Add(this.Columns[11], p.May.ToString() == "0" ? string.Empty : p.May.ToString());
                        ColumnValues.Add(this.Columns[12], p.Jun.ToString() == "0" ? string.Empty : p.Jun.ToString());
                        ColumnValues.Add(this.Columns[13], p.Jul.ToString() == "0" ? string.Empty : p.Jul.ToString());
                        ColumnValues.Add(this.Columns[14], p.Aug.ToString() == "0" ? string.Empty : p.Aug.ToString());
                        ColumnValues.Add(this.Columns[15], p.Sep.ToString() == "0" ? string.Empty : p.Sep.ToString());
                        ColumnValues.Add(this.Columns[16], p.Oct.ToString() == "0" ? string.Empty : p.Oct.ToString());
                        ColumnValues.Add(this.Columns[17], p.Nov.ToString() == "0" ? string.Empty : p.Nov.ToString());
                        ColumnValues.Add(this.Columns[18], p.Dec.ToString() == "0" ? string.Empty : p.Dec.ToString());
                        ColumnValues.Add(this.Columns[19], p.Total.ToString() == "0" ? string.Empty : p.Total.ToString());
                        ColumnValues.Add(this.Columns[20], Convert.ToDouble(p.ScoreAchieved).ToString("0.00"));
                        ColumnValues.Add(this.Columns[21], p.KPIAssignID.ToString());
                        collection.Add(idx, ColumnValues);
                    }
                }
            }
            catch (Exception ex)
            {
                objSysLog = new sysLog();
                objSysLog.Description = ex.Message;
                objSysLog.LogEvent = "Load Data Grid";
                objSysLog.Source = "DashboardSerializer.ashx";
                objSysLog.CreatedBy = User.Email;
                objSysLog.CreatedDate = DateTime.Now;
                sysLogFacade.Save(objSysLog);
            }


            IList<IDictionary<string, string>> aaData = collection.Values.ToList();

            var gridData = new
            {
                sEcho,
                iTotalRecords,
                iTotalDisplayRecords,
                aaData,
                EmployeeName,
                TotalWeight,
                TotalScore
            };
            var JSONSerializer = new JavaScriptSerializer();
            return JSONSerializer.Serialize(gridData);
        }
    }
}